package model.objects;

import java.awt.Graphics;

import javax.swing.JPanel;

import model.general.Location;
import model.general.Util;
import model.move.IMove;
import model.move.MoveItems;
import model.observer.Obsever;

public class Lighting extends Sprites implements IAnimateItems {
	private IMove move = new MoveItems(location);
	private Boat boat;
	private int step = Util.DEFAULT_SPEED_MOVE;
	private boolean isMoveLeft = false;
	private boolean isHitBoat = false;
	private int minX = 0;
	private int maxX = Util.DEFAULT_WIDTH_PANEL;

	public Lighting(Location location, String srcImgLeft, String srcImgRight,
			JPanel pnl, Boat boat) {
		super(location, srcImgLeft, srcImgRight, pnl);
		this.boat = boat;
		System.out.println(boat.getLocation() + "---- Location Boat");
	}

	@Override
	public void move() {
		chooseOrientation();
		hasHitBoat();
		if (isMoveLeft) {
			move.moveLeft();
		} else {
			move.moveRight();
		}
	}

	private void chooseOrientation() {
		if (location.getX() < minX) {
			isMoveLeft = false;
		}
		if ((location.getX() + width) > maxX) {
			isMoveLeft = true;
		}
	}
	
	public void hasHitBoat(){
		if (location.getX() > boat.getLocation().getX()
				&& location.getX() < (boat.getLocation().getX()+boat.getWidth())) {
			double i = Math.random();
			System.out.println(i);
			if ( i < 0.02) {
				isHitBoat = true;
			}
		}
	}

	@Override
	public void show(Graphics g) {
		g.drawImage(img, location.getX(), location.getY(), null);
		if(isHitBoat) hitBoat(g);
	}
	
	public void hitBoat(Graphics g){
		img = loadImage.getbufferImage(srcImgRight);
		g.drawImage(img,location.getX(),location.getY(),null);
		//tru life cua boat
		boat.addLife(-1);
		img = loadImage.getbufferImage(srcImgLeft);
		isHitBoat = false;
	}

	@Override
	public void updateLocation(Location loc) {
		this.location.setX(loc.getX());
		this.location.setY(loc.getY());
	}

	@Override
	public void destroy() {

	}

	@Override
	public Obsever getObserver() {
		return null;
	}

	@Override
	public void setMove(IMove move) {
		this.move = move;
	}

	@Override
	public void setSpeed(int step) {
		this.step = step;
		this.move.setSpeed(step);
	}

	@Override
	public void setMove(boolean ismove) {
		// TODO Auto-generated method stub
		
	}

}
